<template>
	<el-container>
		<el-container>
			<el-header>
				<div class="left-panel">
					<div style="font-size: 16px;">选择打印模板：</div>
					<el-select v-model="form.template_id" clearable filterable placeholder="请选择模板" @change="templateChange">
						<el-option v-for="item in templateList" :key="item.id" :label="item.name" :value="item.id"/>
					</el-select>
				</div>
				<div class="right-panel">
					<div class="right-panel-search">
						<el-button type="success" plain @click="toPrint">打印</el-button>
						<el-button type="success" plain @click="preview">预览</el-button>
<!--						<el-button type="success" plain @click="preview">下载</el-button>-->
						<el-button @click="close">返回</el-button>
					</div>
				</div>
			</el-header>
			<el-main>
				<sc-editor-print
					ref="editor"
					v-model="content"
					placeholder="请输入"
				></sc-editor-print>
			</el-main>
		</el-container>
	</el-container>
</template>
<script>
import tinymce from 'tinymce/tinymce'
import useTabs from '@/utils/useTabs'
import store from "@/store";

export default {
	name: '打印模板',
	components: {},
	data() {
		return {
			cursorPosition: null,
			form:{
				id:1,
				template_id:'',
			},
			templateList:[],
			MenuName:'',
      content:''
		}
	},
	mounted() {
		let MenuName = store.state.print.MenuName
		if (MenuName) {
			this.MenuName = MenuName
		}
		this.getTemplate()
	},
	computed: {

	},
	watch: {

	},
	beforeUnmount() {
		console.log('销毁')
		tinymce.remove()
	},
	methods: {
		/**模板选中事件*/
		templateChange() {
			this.setData()
		},
		/**获取打印模板*/
		async getTemplate() {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// Object.assign(this.form, data)
			var res = await this.$API.print.template.all.get({MenuName:this.MenuName});
			this.templateList = res.data
			if(this.templateList.length > 0) {
				this.form.template_id = this.templateList[0].id
				this.setData()
			}
		},
		/**返回*/
		close() {
			useTabs.close()
		},
		//表单注入数据
		async setData() {
			//可以和上面一样单个注入，也可以像下面一样直接合并进去
			// Object.assign(this.form, data)
			var res = await this.$API.print.template.printContant.get(this.form);
			this.content = res.data.content
			this.handleContent()
		},
		changeHtml(html) {
			this.content = html
		},
		/**打印按钮点击*/
		toPrint() {
			tinymce.activeEditor.execCommand('mceprint');
		},
		/**预览*/
		preview() {
			tinymce.activeEditor.execCommand('mcePreview');
		},
		handleContent() {
			tinymce.activeEditor.setContent(this.content)
			this.$nextTick(() => {
				if(this.$refs.editor && this.$refs.editor.EditorOnsize) {
					this.$refs.editor.EditorOnsize()
				}
			})
		},
	}
}
</script>

